<?php

defined('BASEPATH') or die('No direct script access allowed');

/**
 * 记账任务
 * @author  Karson
 * @name    task.php
 * @since   2013-7-29 21:21:17
 */
class Task_Controller extends Controller {

    private $state_list = array();

    function _init() {
        check_level();
        $this->state_list = array(
            0 => '<font color=gray>禁用</font>', 1 => '<font color=green>正常</font>', -1 => '<font color=purple>已过期</font>', -2 => '<font color=blue>已结束</font>',
        );
    }

    function index($page = 1, $pagesize = 5) {
        $this->lists($page, $pagesize);
    }

    function lists($page = 1, $pagesize = 5) {
        $where = array();
        if ($this->auth->fields['family_id']) {
            $familyids = $this->family->get_family_ids();
            $where[] = "user_id='{$this->auth->user_id}' OR (family='1' AND user_id IN (" . (implode(",", $familyids)) . "))";
        } else {
            $where[] = "user_id='{$this->auth->user_id}'";
        }
        $where = implode(" AND ", $where);
        $result = $this->db->one("SELECT COUNT(*) AS num FROM {pre}task WHERE {$where}");
        $total = $result['num'];
        $offset = ($page - 1) * $pagesize;
        $pagelist = $this->page->get("/task/list/{page}/{$pagesize}/", $total, $page, $pagesize, false, false);

        $list = array();
        $this->db->query("SELECT * FROM {pre}task WHERE {$where} ORDER BY id DESC LIMIT {$offset},{$pagesize}", "list");
        while ($row = $this->db->get_array("list")) {
            $row['category'] = get_category($row['category_id']);
            if (isset($row['category']['father_id'])) {
                $row['father_category'] = get_category($row['category']['father_id']);
            } else {
                $row['father_category'] = array('id' => 0, 'name' => '未知');
            }
            $row['account'] = get_account($row['account_id']);
            $row['inaccount'] = get_account($row['inaccount_id']);
            $row['memo'] = cutstr($row['memo'], 40, '...');
            $row['statetext'] = isset($this->state_list[$row['state']]) ? $this->state_list[$row['state']] : '未知';
            $list[] = $row;
        }
        $data = array("pagelist" => $pagelist, 'list' => $list);
        render("task/index", $data);
    }

    function edit($id) {
        $show = $this->db->one("SELECT * FROM {pre}task WHERE id='{$id}'");
        if ($show) {
            if ($show['user_id'] == $this->auth->user_id) {
                if ($this->form->is_submit()) {
                    $data = $this->input->post("row");
                    $value['starttime'] = strtotime($data['starttime']);
                    $value['expiretime'] = $data['expiretime'] ? strtotime($data['expiretime']) : 0;
                    $value['amount'] = (float) $data['amount'];
                    $value['title'] = $data['title'] != '' ? $data['title'] : '未命名定时任务';
                    $value['memo'] = $data['memo'];
                    $value['family'] = isset($data['notinfamily']) ? 0 : 1;
                    $value['maximums'] = $data['maximums'];
                    $value['frequency'] = $data['frequency'];
                    $value['state'] = $data['state'];
                    $id = $this->db->update("{pre}task", $value, array('id' => $id));
                    $status = 'y';
                    $info = '操作成功!';
                    dialog_output($status, $info);
                }
                $this->view->assign("show", $show);
                $this->view->assign("frequency_list", $this->form->select("row[frequency]", get_frequency_list(), $show['frequency'], array('class' => 'select')));
                $this->view->assign("state_list", $this->form->select("row[state]", $this->state_list, $show['state'], array('class' => 'select')));
                render("task/edit");
            } else {
                render("error", '不能进行越权操作');
            }
        } else {
            render("error", '记录未找到');
        }
    }

    function del($id) {
        $task = $this->db->one("SELECT * FROM {pre}task WHERE id='{$id}'");
        if ($task) {
            //删除
            $this->db->query("DELETE FROM {pre}task WHERE id = '{$id}' AND user_id='{$this->auth->user_id}'");
            $code = 0;
            $content = '删除成功';
        } else {
            $code = -1;
            $content = '帐户未找到';
        }
        json_output($code, $content);
    }

}